/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

import java.util.Map;

/**
 *
 * @author Laurent WOUTERS
 */
public class IconProvider {
    private static Map<String, javax.swing.ImageIcon> embedded = new java.util.HashMap<>();
    private static Map<String, javax.swing.ImageIcon> external = new java.util.HashMap<>();
    private static Map<String, javax.swing.ImageIcon> externalByHash = new java.util.HashMap<>();
    
    public static javax.swing.ImageIcon getEmbedded(String name) {
        if (embedded.containsKey(name))
                return embedded.get(name);
        return loadEmbedded(name);
    }
    private static javax.swing.ImageIcon loadEmbedded(String name) {
        java.lang.ClassLoader loader = java.lang.ClassLoader.getSystemClassLoader();
        java.net.URL url = loader.getResource("xowl/gmi/resources/icons/" + name);
        if (url == null)
            return null;
        javax.swing.ImageIcon icon = new javax.swing.ImageIcon(url);
        embedded.put(name, icon);
        return icon;
    }
    
    public static javax.swing.ImageIcon getExternal(String name) {
        if (external.containsKey(name))
                return external.get(name);
        return loadExternal(name);
    }
    public static javax.swing.ImageIcon getExternalByHash(String hash) {
        return externalByHash.get(hash);
    }
    private static javax.swing.ImageIcon loadExternal(String name) {
        javax.swing.ImageIcon icon = new javax.swing.ImageIcon(name);
        external.put(name, icon);
        externalByHash.put(Integer.toString(icon.hashCode()), icon);
        return icon;
    }
}
